//
//  KTCoinDetailPankouView.swift
//  KOLToken
//
//  Created by san.yang on 2020/3/8.
//  Copyright © 2020 iOS-team. All rights reserved.
//

import UIKit
import DynamicColor

/// 全网指数
class KTCoinDetailWholePankouView: UIView {
    
    // MARK: - private
    private let kKTCoinDetailCollectionViewCell = "kKTCoinDetailCollectionViewCell"

    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        buildUI()
    }
    
    private func buildUI() {
        backgroundColor = DynamicColor(hex: 0x383838)
        addSubview(nameLabel)
        addSubview(numLabel)
        addSubview(dollarLabel)
        addSubview(RMBLabel)
        addSubview(zdfLabel)
        addSubview(marketValueTitleLabel)
        addSubview(marketValueNumLabel)
        addSubview(pankouCollectionView)
        pankouCollectionView.register(KTCoinDetailCollectionViewCell.self, forCellWithReuseIdentifier: "kKTCoinDetailCollectionViewCell")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
        let leftSpaceing = 10
        let topSpaceing = 16
        let middleSpaceing = 16
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(leftSpaceing)
            make.top.equalTo(topSpaceing)
            make.height.equalTo(14)
        }
        
        numLabel.snp.makeConstraints { (make) in
            make.right.equalTo(-leftSpaceing)
            make.top.centerY.equalTo(nameLabel)
            make.height.equalTo(16)
        }
        
        dollarLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp_bottom).offset(middleSpaceing)
            make.height.equalTo(20)
        }
        
        RMBLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(dollarLabel.snp_bottom).offset(middleSpaceing)
            make.height.equalTo(14)
        }
        
        zdfLabel.snp.makeConstraints { (make) in
            make.left.equalTo(RMBLabel.snp_right).offset(18)
            make.top.height.equalTo(RMBLabel)
        }
        
        marketValueTitleLabel.snp.makeConstraints { (make) in
            make.right.height.equalTo(numLabel)
            make.top.equalTo(nameLabel.snp_bottom).offset(middleSpaceing)
        }
        
        marketValueNumLabel.snp.makeConstraints { (make) in
            make.right.height.equalTo(marketValueTitleLabel)
            make.top.equalTo(marketValueTitleLabel.snp_bottom).offset(middleSpaceing)
        }
        
        pankouCollectionView.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.right.equalTo(numLabel)
            make.bottom.equalToSuperview()
            make.height.equalTo(90)
        }
    }
    
    // MARK: - lazy
    
    private lazy var nameLabel: UILabel = {
        let label = FastLabel().fastCreateLabel(text: "QRL", color: nil, alignment: .left, textColor: .white, font: UIFont.systemFont(ofSize: 16))
        return label
    }()
    
    private lazy var numLabel: UILabel = {
        let label = FastLabel().fastCreateLabel(text: "No.267", color: nil, alignment: .left, textColor: .white, font: UIFont.systemFont(ofSize: 16))
        return label
    }()
    
    private lazy var dollarLabel: UILabel = {
        let label = FastLabel().fastCreateLabel(text: "$0.00002", color: nil, alignment: .left, textColor: .white, font: UIFont.systemFont(ofSize: 16))
        return label
    }()
    
    private lazy var RMBLabel: UILabel = {
        let label = FastLabel().fastCreateLabel(text: "0.1098", color: nil, alignment: .left, textColor: .white, font: UIFont.systemFont(ofSize: 16))
        return label
    }()
    
    private lazy var zdfLabel: UILabel = {
        let label = FastLabel().fastCreateLabel(text: "+10.0%", color: nil, alignment: .left, textColor: .white, font: UIFont.systemFont(ofSize: 16))
        return label
    }()
    
    private lazy var marketValueTitleLabel: UILabel = {
        let label = FastLabel().fastCreateLabel(text: "市值", color: nil, alignment: .left, textColor: .white, font: UIFont.systemFont(ofSize: 16))
        return label
    }()
    
    private lazy var marketValueNumLabel: UILabel = {
        let label = FastLabel().fastCreateLabel(text: "2332亿", color: nil, alignment: .left, textColor: .white, font: UIFont.systemFont(ofSize: 16))
        return label
    }()
    
    private lazy var pankouCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize(width: (KTAppFrame.kScreenWidth - 20 - 10) / 2, height: 30)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 10
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        return collectionView
    }()
}

extension KTCoinDetailWholePankouView: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 6
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:KTCoinDetailCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: kKTCoinDetailCollectionViewCell, for: indexPath) as! KTCoinDetailCollectionViewCell
        return cell
    }
}


class KTCoinDetailCollectionViewCell: UICollectionViewCell {
    // MARK: - init
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        buildUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func buildUI() {
        addSubview(leftLabel)
        addSubview(rightLabel)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        leftLabel.snp.makeConstraints { (make) in
            make.left.top.bottom.equalToSuperview()
            make.width.equalTo(self.width / 2)
        }
        rightLabel.snp.makeConstraints { (make) in
            make.right.top.bottom.equalToSuperview()
            make.width.equalTo(self.width / 2)
        }
    }
    
    // MARK: - lazy
    lazy var leftLabel: UILabel = {
        let label = FastLabel().fastCreateLabel(text: "净流入", color: nil, alignment: .left, textColor: .black, font: UIFont.systemFont(ofSize: 16))
        return label
    }()
    
    lazy var rightLabel: UILabel = {
        let label = FastLabel().fastCreateLabel(text: "$1212", color: nil, alignment: .right, textColor: .black, font: UIFont.systemFont(ofSize: 16))
        return label
    }()
}
